<script type="text/javascript">                    
    $(document).ready(function() {
        $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
        $("#datepicker2").datepicker({dateFormat: 'yy-mm-dd'});
      });                 
</script>
<div class="box">
        <!-- box / title -->
        <div class="title">
                <h5>Manajemen Group</h5>
                <ul class="links">
                        <!-- <li><a href="<?=base_url();?>index.php/group_management">New</a></li>
                        <li><a href="<?=base_url()."index.php/".$this->uri->segment(1)?>">List Data</a></li> -->
                </ul>
        </div>
        <!-- end box / title -->

    <?php
       $msg = $this->session->flashdata('message');
        if($msg <> ""){
              echo'<div id="box-messages">
                        <div class="messages">
                                <div id="message-success" class="message message-success">
                                        <div class="image">
                                                <img src="'. base_url() .'file/shell/smooth/resources/images/icons/success.png" alt="Success" height="32" />
                                        </div>
                                        <div class="text">
                                                <h6>'. $msg .'</h6>
                                        </div>
                                        <div class="dismiss">
                                                <a href="#message-success"></a>
                                        </div>
                                </div>
                        </div>
                </div>';
        }
        ?>
        <?php 
                if($user_group_id!="")
                { ?>
                        <form id="form" action="<?=$_SERVER['PHP_SELF']?>/update" method="post">
                <?php } else { ?>
                        <form id="form" action="<?=$_SERVER['PHP_SELF']?>/add" method="post">
                <?php } ?>
                    <div class="form">
                        <div class="fields">

                            <div class="field">
                                <div class="label">
                                    <label for="group_name">Nama Group :</label>
                                </div>
                                <div class="input">
                                    <input type="text" id="group_name" name="user_group_name" size="25" value="<?php echo $user_group_name;?>"/><span>Contoh : Direct Sales</span>
                                </div>
                            </div>

                            <div class="field">
                                <div class="label">
                                    <label for="caption">Caption :</label>
                                </div>
                                <div class="input">
                                    <input type="text" id="caption" name="caption" size="5" value="<?php echo $user_group_caption;?>"/><span>Contoh : SFA, DS, CC, HO, dll</span>
                                </div>
                            </div>
                            
                            <div class="field">
                                <div class="label">
                                    <label for="territory_type_id">Tipe Teritori :</label>
                                </div>
                                <div class="input">
                                    <select id="territory_type_id" name="territory_type_id">
                                        <?php
                                                                        foreach ($user_group_data as $r) {
                                                                             if($r->territory_type_id == $territory_type_id)
                                                                             {   
                                                                                echo '<option value="'.$r->territory_type_id.'" selected>'.$r->territory_type_name.'</option>';

                                                                             }
                                                                             else 
                                                                             {
                                                                                echo '<option value="'.$r->territory_type_id.'">'.$r->territory_type_name.'</option>';
                                                                             }
                                                                        }
                                                                ?>
                                    </select>
                                </div>
                            </div>

                            <div class="field">
                                <div class="label">
                                    <label for="parent_id">Parent Group :</label>
                                </div>
                                <div class="input">
                                    <select id="parent_id" name="parent_id">
                                        <?php
                                                                        foreach ($parent_group_data as $r) {
                                                                             if($r->user_group_id == $parent_id)
                                                                             {   
                                                                                echo '<option value="'.$r->user_group_id.'" selected>'.$r->user_group_name.'</option>';
                                                                             }
                                                                             else 
                                                                             {
                                                                                echo '<option value="'.$r->user_group_id.'">'.$r->user_group_name.'</option>';
                                                                             }
                                                                        }
                                                                ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="field">
                                <div class="label">
                                    <label for="istatus">Status : </label>
                                 </div>
                                <div class="input">
                                    <?php
                                        if($istatus ==1)
                                            { ?>
                                        <input type="checkbox" id="istatus" name="istatus" checked><span>Aktif</span>
                                        
                                    <?php
                                        }else{
                                    ?>
                                        <input type="checkbox" id="istatus" name="istatus"><span>Aktif</span>
                                    <?php     } ?>
                                </div>
                            </div>

                            <div class="field">
                                <div class="label">  
                                    <label for="having_stock">Apakah merupakan sales?<br>
                                        (yang mempunyai stock)</label>
                                </div>
                                <div class="input">
                                    <?php
                                        if($having_stock ==1)
                                            { ?>
                                        <input type="checkbox" id="having_stock" name="having_stock" checked>
                                    <?php
                                        }else{
                                    ?>
                                        <input type="checkbox" id="having_stock" name="having_stock">
                                    <?php     } ?>
                                </div>
                            </div>

                            <div class="field">
                                <div class="label"> 
                                    <label for="is_use_top">Apakah boleh memberikan <br> Term of Payment?</label>
                                </div>
                                <div class="input">
                                    <?php
                                        if($is_use_top ==1)
                                            { ?>
                                        <input type="checkbox" id="is_use_top" name="is_use_top" checked>
                                     <?php
                                        }else{
                                    ?>
                                        <input type="checkbox" id="is_use_top" name="is_use_top">
                                    <?php     } ?>
                                </div>
                            </div>

                            <div class="buttons">
                                <?php 
                                                   if($user_group_id != "")
                                                   {
                                                ?>
                                                        <input type="hidden" name="user_group_id" value="<?php echo $user_group_id;?>">
                                                        <input type="submit" name="submit" value="Update Grup" id="submit"/>
                                                <?php
                                                   }
                                                   else
                                                   {
                                                ?>
                                                        <input type="submit" name="submit" value="Tambahkan Grup" id="submit"/>
                                                                   
                                                <?php   } ?> 
                            </div>
                        </div>
                    </div>
                </form><br><br>&nbsp;
       
        
       
        <div class="table">
                <base href="<?php echo base_url() ?>" />
                <div id="ajaxLoadAni">
                        <img src="file/smooth/images/ajax-loader.gif" alt="Ajax Loading Animation" />
                        <span>Loading...</span>
                </div>
        
                
<?php   ################################################################################ ?>
<!-- delete confirmation dialog box -->
<div id="delConfDialog" title="Confirm">
    <p>Are you sure?</p>
</div>
<!-- message dialog box -->
<div id="msgDialog"><p></p></div>

<table id="records" class="handsetRecord">
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>

<script type="text/javascript">
var readUrl   = 'index.php/group_management/read',
    updateUrl = 'index.php/group_management',
    delUrl    = 'index.php/group_management/delete',
    delHref,
    updateHref,
    updateId;


$( function() {
    
    readUsers();
    $( '#msgDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
            }
        }
    });
    
    $( '#delConfDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'No': function() {
                $( this ).dialog( 'close' );
            },
            'Yes': function() {
                //display ajax loader animation here...
                $( '#ajaxLoadAni' ).fadeIn( 'slow' );
                $( this ).dialog( 'close' );
                $.ajax({
                    url: delHref,
                    success: function( response ) {
                        //hide ajax loader animation here...
                        $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                        
                        $( '#msgDialog > p' ).html( response );
                        $( '#msgDialog' ).dialog( 'option', 'title', 'Success' ).dialog( 'open' );
                        
                        $( 'a[href=' + delHref + ']' ).parents( 'tr' )
                        .fadeOut( 'slow', function() {
                            $( this ).remove();
                        });
                        
                    } //end success
                });
            } //end Yes
        } //end buttons
    }); //end dialog
    
    
    $( '#records' ).delegate( 'a.deleteBtn', 'click', function() {
        delHref = $( this ).attr( 'href' );
        $( '#delConfDialog' ).dialog( 'open' );
            return false;
    }); //end delete delegate
    // --- Create Record with Validation ---
    
}); //end document ready


function readUsers() {
    $('.handsetRecord').dataTable({
                "bJQueryUI": true, // aktifkan tampilan tabel JQueryUI
                "sAjaxSource": readUrl, //data source
                "sAjaxDataProp": "aaData", //variabel yg menampung data table
                "bServerSide": true, //serverside processing true
                "bProcessing": true, // tampilkan 'processing...' status saat data dipanggil
                // "sPaginationType": 'full_numbers',
                "bPaginate": true,
                "aoColumns":[ //tentukan kolom pd tabel yg akan menampung value dari server
                    {"mDataProp": "id_grup", "sTitle": "ID Grup"}, 
                    {"mDataProp": "nama_grup", "sTitle": "Nama Grup"}, 
                    {"mDataProp": "caption", "sTitle": "Caption"},
                    {"mDataProp": "user_group_name", "sTitle": "Nama User Grup"},
                    {"mDataProp": "teritori", "sTitle": "Teritori"},
                    {"mDataProp": "status", "sTitle": "Status"}
                ],
                "fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) 
                {
                    $(nRow).find('td').eq(-1).after('<td><a href="'+updateUrl+'?update_group_id='+aaData[0]+'">Update</a> | <a href="'+delUrl+'/'+aaData[0]+'" class="deleteBtn">Delete</a></td>');
                    $status = $(nRow).find('td').eq(5).html();
                    if($status == '1')
                    {
                        $(nRow).find('td').eq(5).html('aktif');
                    }
                    else
                    {
                        $(nRow).find('td').eq(5).html('non-aktif');
                    }
                    // $(nRow).find('td').eq(-1).after("<td><a href='"+updateUrl+"?update_group_id='"+<?=$this->encrypt->encode('123')?>+"'>Update</a> | <a href='"+delUrl+"/"+aaData[0]+"' class='deleteBtn'>Delete</a></td>");

                    $.ajax({
                               url: "<?=base_url();?>index.php/group_management/defoger",
                               type: "POST",
                               data: {'str' : aaData[0]},
                               success: function(response) {
                                   $(nRow).find('td').eq(0).replaceWith(response);
                               }
                        });

                    return nRow;
                }
        });
        $('.handsetRecord thead tr').find('th').eq(-1).after('<th class="ui-state-default" width="200px">Action</th>');
} // end readUsers
</script>
<?php   ################################################################################ ?>

        </div>
</div>